<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="企业名称" prop="refComNo">
        <el-select v-model="queryParams.refComNo" filterable placeholder="请选择企业名称" clearable size="small">
          <el-option
            v-for="dict in companiesName"
            :key="dict.comId"
            :label="dict.comName"
            :value="dict.comId"
            @keyup.enter.native="handleQuery"/>
        </el-select>
      </el-form-item>
      <el-form-item label="银行名称" prop="refBankId">
        <el-select v-model="queryParams.refBankId" placeholder="请选择银行名称" filterable clearable size="small">
          <el-option
            v-for="dict in bankNames"
            :key="dict.bankId"
            :label="dict.bankName"
            :value="dict.bankId"
            @keyup.enter.native="handleQuery"/>
        </el-select>
      </el-form-item>
      <el-form-item label="贷款编号" prop="refLoId">
        <el-select v-model="queryParams.refLoId" placeholder="请选择贷款编号" filterable clearable size="small">
          <el-option
            v-for="dict in loansNoOptions"
            :key="dict.loaId"
            :label="dict.loaNo"
            :value="dict.loaId"
            @keyup.enter.native="handleQuery"/>
        </el-select>
      </el-form-item>
      <el-form-item label="逾期ID" prop="refOveId">
        <el-input
          v-model="queryParams.refOveId"
          placeholder="请输入逾期ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="还款截止时间" prop="refTime" >
        <el-date-picker clearable size="small"
                        v-model="queryParams.refTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择还款截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="还款金额" prop="refMoney">
        <el-input
          v-model="queryParams.refMoney"
          placeholder="请输入还款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="还款方式" prop="refMode">
        <el-select v-model="queryParams.refMode" placeholder="请选择还款方式" clearable size="small">
          <el-option
            v-for="dict in bankSendModes"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="还款状态" prop="refState">
        <el-select v-model="queryParams.refState" placeholder="请选择还款状态" clearable size="small">
          <el-option
            v-for="dict in refundStatus"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:refund:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:refund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="refId" />
      <el-table-column label="企业名称" align="center" prop="comName" />
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="贷款编号" align="center" prop="loaNo" />
      <el-table-column label="还款截止时间" align="center" prop="refTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="还款金额" align="center" prop="refMoney" />
      <el-table-column label="还款方式" align="center" prop="refMode" >
        <template slot-scope="scope">
          <dict-tag :options="bankSendModes" :value="scope.row.refMode"/>
        </template>
      </el-table-column>
      <el-table-column label="还款状态" align="center" prop="refState" >
        <template slot-scope="scope">
          <dict-tag :options="refundStatus" :value="scope.row.refState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:refund:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:refund:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改还款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="企业名称" prop="refComNo">
          <el-select v-model="form.refComNo" placeholder="请选择企业名称" filterable>
            <el-option
              v-for="dict in companiesName"
              :key="dict.comId"
              :label="dict.comName"
              :value="dict.comId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="银行名称" prop="refBankId">
          <el-select v-model="form.refBankId" placeholder="请选择银行名称" filterable>
            <el-option
              v-for="dict in bankNames"
              :key="dict.bankId"
              :label="dict.bankName"
              :value="dict.bankId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="贷款编号" prop="refLoId">
          <el-select v-model="form.refLoId" placeholder="请选择贷款编号" filterable>
            <el-option
              v-for="dict in loansNoOptions"
              :key="dict.loaId"
              :label="dict.loaNo"
              :value="dict.loaId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="还款截止时间" prop="refTime">
          <el-date-picker clearable size="small"
                          v-model="form.refTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择还款截止时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="还款金额" prop="refMoney">
          <el-input v-model="form.refMoney" placeholder="请输入还款金额" />
        </el-form-item>
        <el-form-item label="还款方式" prop="refMode">
          <el-select v-model="form.refMode" placeholder="请选择还款方式">
            <el-option
              v-for="dict in bankSendModes"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="还款状态" prop="refState">
          <el-select v-model="form.refState" placeholder="请选择还款状态" @change="changeStatus1">
            <el-option
              v-for="dict in refundStatus"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="实际还款时间" prop="refActTime" >
          <el-date-picker clearable size="small"
                          v-model="form.refActTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择实际还款时间"
                          :picker-options="pickerOptions"   :disabled="timdDisable"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, getRefund, delRefund, addRefund, updateRefund, exportRefund } from "@/api/system/refund";

export default {
  name: "Refund",

  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      refundList: [],
      refundStatus: [],
      bankSendModes: [],
      loansNoOptions: [],
      companiesName: [],
      bankNames: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refComNo: null,
        refBankId: null,
        refLoId: null,
        refOveId: null,
        refTime: null,
        refMoney: null,
        refMode: null,
        refState: null,
      },
      form: {
        refId: null,
        refComNo: null,
        refBankId: null,
        refLoId: null,
        refOveId: null,
        refTime: null,
        refMoney: null,
        refMode: null,
        refState: null,
        refDel: null,
        refActTime: null
      },
      rules: {
        refComNo:[{ required: true, message: "企业名称不能为空", trigger: "blur" }],
        refBankId:[{ required: true, message: "银行名称不能为空", trigger: "blur" }],
        refLoId:[{ required: true, message: "贷款编号不能为空", trigger: "blur" }],
        refMode:[{ required: true, message: "还款方式不能为空", trigger: "blur" }],
        refState:[{ required: true, message: "还款状态不能为空", trigger: "blur" }],
      },
      timdDisable:true,
      pickerOptions:{
        disabledDate:(time)=>{
          return time.getTime() >= Date.now() - 8.64e6;
        }
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_refund_status").then(response => {
      this.refundStatus = response.data;
    });
    this.getDicts("sys_send_mode").then(response => {
      this.bankSendModes = response.data;
    });
  },
  methods: {
    changeStatus1(value){
      this.timdDisable = value !== 2;
    },

    /** 查询还款列表（核心：双重深拷贝确保数据独立） */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        // 双重处理：先深拷贝再打散对象，彻底切断引用
        this.refundList = JSON.parse(JSON.stringify(response.rows || [])).map(item => ({
          ...item,
          refTime: item.refTime ? new Date(item.refTime).toISOString() : null,
          refActTime: item.refActTime ? new Date(item.refActTime).toISOString() : null
        }));
        this.total = response.total || 0;

        // 下拉选项同样隔离处理
        this.companiesName = [...new Map(
          this.refundList.map(item => [
            item.comId,
            { ...item, comId: item.comId, comName: item.comName }
          ])
        ).values()];

        this.bankNames = [...new Map(
          this.refundList.map(item => [
            item.bankId,
            { ...item, bankId: item.bankId, bankName: item.bankName }
          ])
        ).values()];

        this.loansNoOptions = [...new Map(
          this.refundList.map(item => [
            item.loaId,
            { ...item, loaId: item.loaId, loaNo: item.loaNo }
          ])
        ).values()];

        this.loading = false;
      });
    },

    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        refId: null,
        refComNo: null,
        refBankId: null,
        refLoId: null,
        refOveId: null,
        refTime: null,
        refMoney: null,
        refMode: null,
        refState: null,
        refDel: null,
        refActTime: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.refId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加还款";
      this.timdDisable = true;
    },
    handleUpdate(row) {
      // 处理表格上方按钮和表格内按钮的参数
      const currentRow = row || (this.ids.length > 0 ? this.refundList.find(item => item.refId === this.ids[0]) : null);
      if (!currentRow) {
        this.msgError("未选择数据，请先选择一行数据");
        return;
      }

      this.reset();
      const refId = currentRow.refId;
      getRefund(refId).then(response => {
        if (response.data) {
          // 表单数据双重隔离
          const rawData = JSON.parse(JSON.stringify(response.data));
          this.form = {
            ...rawData,
            refTime: rawData.refTime ? new Date(rawData.refTime).toISOString() : null,
            refActTime: rawData.refActTime ? new Date(rawData.refActTime).toISOString() : null
          };
          this.open = true;
          this.title = "修改还款";
          this.timdDisable = this.form.refState !== 2;
        } else {
          this.msgError("获取数据失败，后端未返回数据");
        }
      }).catch(error => {
        this.msgError("请求失败：" + error.message);
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 提交数据深拷贝
          const submitData = JSON.parse(JSON.stringify(this.form));

          if (submitData.refId != null) {
            updateRefund(submitData).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              // 延迟刷新确保数据全新
              setTimeout(() => this.getList(), 300);
            });
          } else {
            addRefund(submitData).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              setTimeout(() => this.getList(), 300);
            });
          }
        }
      });
    },
    handleDelete(row) {
      const refIds = row?.refId || this.ids;
      this.$confirm(`是否确认删除还款编号为"${refIds}"的数据项?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => delRefund(refIds))
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有还款数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRefund(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
